Forward algorithm